<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>如何干活</title>
    <base href="/">
    <script src="js/common/jquery.js"></script>
    <script src="js/common/common-functions.js"></script>
    <script src="css/bootstrap/js/bootstrap.bundle.min.js"></script>
    <link href="css/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="css/common.css" rel="stylesheet">
</head>
<body>
<nav class="navbar  common-nav-margin-bottom navbar-expand-lg bg-primary">
    <div class="container">
        <a href="/">
            <img src="asserts/common/mybatis-logo.png" alt="Bootstrap" width="90" height="24">
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup"
                aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
            <div class="navbar-nav">
                <a class="nav-link" href="/">Home</a>
            </div>
        </div>

    </div>
</nav>

<div class="container page-layout">
    <aside class="aside-menu">
        <ul class="list-unstyled">
            <li><a class="aside-menu-link" href="javascript:void(0) "
                   onclick="scrollToElementById('section-0')">怎么干活</a></li>
        </ul>
    </aside>
    <main>
        <h1 id="section-0">0. 怎么干活</h1>
        <div>
            <p>假设现在有一个用户信息需要插入到用户表，有以下仅需要干以下几件事</p>
            <ol class="">
                <li>定义一个用户类（<button type="button" file-src="/java-resource/user/dto/UserInfo.java" show-src
                                           src-name="UserInfo.java"
                                           class="btn btn-link">UserInfo.java</button>），用于传递参数(如果已经存在不用新建);
                </li>
                <li>创建一个<button type="button" file-src="/mapper-resource/user/Userinfo.mapper.xml" show-src
                                    src-name="Userinfo.mapper.xml"
                                    class="btn btn-link">mapper文件</button>
                    （xml格式）并写好<code>namespace(org.issaclu.springbootmybatis.user.mapper.UserInfoMapper)</code>
                    。如果有，不用创建。
                </li>
                <li>在mapper文件中写一段插入sql,并写好ID(<code>insertUser</code>).</li>
                <li>定义一个接口
                    <button type="button" file-src="/java-resource/user/mapper/UserInfoMapper.java" show-src
                        class="btn btn-link" src-name="UserInfoMapper.java" >
                        <code>org.issaclu.springbootmybatis.user.mapper.UserInfoMapper</code>
                    </button>,
                    这个接口通常是与mapper文件一一对应的，接口的完整名字就是mapper的namespace, 如果有已经存在不用新建。
                </li>
                <li>在接口中定义一个方法<code>insertUser</code>(不可重载)，方法名字就是 前面写的sql的ID。</li>
            </ol>
            <p>以上就是大部分情况下使用mybatis 的过程，之后就是测试</p>

        </div>
        <h2>稍微解释一下</h2>

        <p>mybatis的持久化数据的思路很简单， 就是将最基本的java执行sql的过程模板化，将数据库字段与java实体字段对应。</p>

        <p>java 要持久化数据简单看就3步</p>
        <ol>
            <li>准备要执行的sql</li>
            <li>值入参数</li>
            <li>接收结果集</li>
        </ol>

        <p class="intent-2">第一步是最关键的，因为是java 代码与 sql 的关联步骤，关键问题是如何在调用一个方法时知道要执行哪个sql,
            大概思路就是用接口全名加上方法名作为ID(`org.issaclu.springbootmybatis.user.mapper.UserInfoMapper.insertUser`)
            ,
            去找到mapper中的 sql(`org.issaclu.springbootmybatis.user.mapper.UserInfoMapper.insertUser`)。
        </p>
        <p>例如 `UserInfo`这个例子</p>

        <p>接口如下：</p>
        <pre class="small-light-height-pre">
        package <strong class="emphasize">org.issaclu.springbootmybatis.user.mapper</strong>;

        import org.apache.ibatis.annotations.Mapper;
        import org.issaclu.springbootmybatis.user.dto.UserInfo;

        @Mapper
        public interface <strong class="emphasize">UserInfoMapper</strong> {
        int insertUser(UserInfo userInfo);
        }
        </pre>
        <p>mapper如下</p>
        <div lazy-src src="/mapper-resource/user/UserInfo.mapper.xml" class="small-light-height-pre">

        </div>

        <p><u><strong>注意:</strong>接口全名(<code>org.issaclu.springbootmybatis.user.mapper.UserInfoMapper</code>)与
            namespace <code>org.issaclu.springbootmybatis.user.mapper.UserInfoMapper</code>是一样的</u></p>

        <p>涉及到的相关文件</p>
        <ul>
            <li><code class="emphasize">src/main/resources/mapper/user/Userinfo.mapper.xml</code></li>
            <li><code class="emphasize">src/main/java/org/issaclu/springbootmybatis/user/mapper/UserInfoMapper.java</code></li>
            <li><code>src/main/java/org/issaclu/springbootmybatis/user/service/UserInfoService.java</code></li>
            <li><code>src/main/java/org/issaclu/springbootmybatis/user/dto/UserInfo.java</code></li>
        </ul>
        <p>使用下面接口测试</p>
        <form>
            <div class="mb-3">
                <label for="username" class="form-label">名字</label>
                <input type="text" class="form-control" id="username" aria-describedby="名字">
            </div>
            <div class="mb-3">
                <label for="userAge" class="form-label">年龄</label>
                <input type="number" step="1" class="form-control" id="userAge">
            </div>
        </form>
        <button class="btn btn-primary" onclick="insertUser()">Submit</button>
        <pre id="insertResult">

        </pre>
    </main>
</div>
<script>
    $(function () {
        initSrcEvent()
        lazyLoadSrc()
    });

    function insertUser() {
        let name = $("#username").val();
        let age = $("#userAge").val();
        $.ajax("/user/insertOne", {
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify({userName: name, age}),
            success: function (response) {
                let resultHolder = $("#insertResult");
                resultHolder.html(JSON.stringify(response, null, 4))
                resultHolder[0].scrollIntoView()
            },
            error: function () {
                console.log(arguments)
            }
        })
    }
</script>
</body>
</html>
